#!/usr/bin/env python
#
# Copyright 2008 Allen Hutchison (allen@hutchison.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# $URL: http://severedelays.googlecode.com/svn/trunk/appengine/lib/current_status.py $
__version__ = "$Rev: 61 $"
__author__ = "Allen Hutchison (allen@hutchison.org)"

from lib.status_value import StatusValue

from google.appengine.ext import db

from third_party.memcache_util import gaecache

class CurrentStatus(object):
  def __init__(self):
    self.latest = self.getStatusFromDataStore();
    
  @gaecache(key='UK-LON-DATA', time=(60 * 5))
  def getStatusFromDataStore(self):
    """Retrieves the latest set of data from the datastore
    """
    dbstatus = StatusValue.gql("WHERE geography = :geography "
                               "ORDER BY update DESC LIMIT 11",
                               geography='UK-LON')
    data = []
    for x in dbstatus:
      data.append(x)
    return data

  def getLines(self):
    return self.latest
  
  def getDict(self):
    status = {}
    for x in self.getLines():
      status[x.name] = {}
      status[x.name]['status'] = x.status
      status[x.name]['description'] = x.description
      status[x.name]['update'] = x.update.isoformat()
    return status
